package com.diploma.project.aiqiyi.utils;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.exception.ExceptionUtils;
import org.apache.commons.lang3.StringUtils;

import java.io.UnsupportedEncodingException;
import java.util.Base64;
import java.util.UUID;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@Slf4j
public class StringUtil extends StringUtils {

    public static final String EMPTY = "";
    public static final String SPACE = " ";
    public static final String TAB = "	";

    public static final String ACUTE = "`";
    public static final String TILDE = "~";
    public static final String EXCLAMATION = "!";
    public static final String AT = "@";
    public static final String NUMBER = "#";
    public static final String DOLLAR = "$";
    public static final String PERCENT = "%";
    public static final String CARET = "^";
    public static final String AND = "&";
    public static final String STAR = "*";
    public static final String LEFT_PARENTHESIS = "(";
    public static final String RIGHT_PARENTHESIS = ")";
    public static final String HYPHEN = "-";
    public static final String UNDERSCORE = "_";
    public static final String EQUALS = "=";
    public static final String PLUS = "+";

    public static final String LEFT_SQUARE_BRACKET = "[";
    public static final String RIGHT_SQUARE_BRACKET = "]";
    public static final String LEFT_CURLY_BRACE = "{";
    public static final String RIGHT_CURLY_BRACE = "}";
    public static final String BACKSLASH = "\\";
    public static final String VERTICAL_BAR = "|";
    public static final String SEMI_COLON = ";";
    public static final String COLON = ":";
    public static final String SINGLE_QUOTATION = "'";
    public static final String DOUBLE_QUOTATION = "\"";
    public static final String COMMA = ",";
    public static final String DOT = ".";
    public static final String LEFT_ANGLE_BRACKET = "<";
    public static final String RIGHT_ANGLE_BRACKET = ">";
    public static final String SLASH = "/";
    public static final String QUESTION = "?";

    /**
     * 将string转为long类型，若string为null、空，或者无法解析为数字，则返回给定的默认值
     *
     * @param longStr
     * @param defaultValue
     * @return
     */
    public static long parseLong(String longStr, long defaultValue) {
        try {
            return Long.parseLong(longStr);
        } catch (NumberFormatException e) {
            return defaultValue;
        }
    }

    /**
     * 将string转为int类型，若string为null、空，或者无法解析为数字，则返回给定的默认值
     *
     * @param intStr
     * @param defaultValue
     * @return
     */
    public static int parseInt(String intStr, int defaultValue) {
        try {
            return Integer.parseInt(intStr);
        } catch (NumberFormatException e) {
            return defaultValue;
        }
    }

    /**
     * 获取uuid字符串
     *
     * @return
     */
    public static String generateUUIDStr() {
        String uuid = UUID.randomUUID().toString();
        // 去掉“-”符号
        uuid = uuid.replace("-", "");
        return uuid;
    }

    /**
     * redis值是否为空
     *
     * @param cs
     * @return
     */
    public static boolean isRedisBlank(String cs) {
        if (isBlank(cs) || "null".equals(cs.trim())) {
            return true;
        }
        return false;
    }

    /**
     * 根据指定的正则表达式，对字符串进行校验
     *
     * @param cs
     * @param regex
     * @return
     */
    public static boolean verifyString(String cs, String regex) throws NullPointerException {
        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher(cs);
        return matcher.matches();
    }

    /**
     * base64加密
     *
     * @param src
     * @return
     */
    public static String getBase64(String src) {
        byte[] b = null;
        String res = null;
        try {
            b = src.getBytes("utf-8");
        } catch (UnsupportedEncodingException e) {
            log.error(ExceptionUtils.getStackTrace(e));
        }

        if (b != null) {
            res = Base64.getEncoder().encodeToString(b);
        }
        return res;
    }

    /**
     * base64解密
     *
     * @param src
     * @return
     */
    public static String getFromBase64(String src) {
        byte[] b = null;
        String res = null;

        if (src != null) {
            b = Base64.getDecoder().decode(src);
        }

        if (b != null) {
            try {
                res = new String(b, "utf-8");
            } catch (UnsupportedEncodingException e) {
                log.error(ExceptionUtils.getStackTrace(e));
            }
        }
        return res;
    }

    public static String encryptUserName(String signName) {
        char[] m = signName.toCharArray();
        for (int i = 0; i < m.length - 1; i++) {
            m[i] = '*';
        }
        return String.valueOf(m);
    }

    /** 下划线转驼峰 */
    public static String lineToHump(String str) {
        Pattern linePattern = Pattern.compile("_(\\w)");
        str = str.toLowerCase();
        Matcher matcher = linePattern.matcher(str);
        StringBuffer sb = new StringBuffer();
        while (matcher.find()) {
            matcher.appendReplacement(sb, matcher.group(1).toUpperCase());
        }
        matcher.appendTail(sb);
        return sb.toString();
    }

    /** 驼峰转下划线 */
    public static String humpToLine(String str) {
        Pattern humpPattern = Pattern.compile("[A-Z]");
        Matcher matcher = humpPattern.matcher(str);
        StringBuffer sb = new StringBuffer();
        while (matcher.find()) {
            matcher.appendReplacement(sb, "_" + matcher.group(0).toLowerCase());
        }
        matcher.appendTail(sb);
        return sb.toString();
    }

    /**
     * 去除空格和换行
     * @param str
     * @return
     */
    public static String replaceBlank(String str) {
        String dest = "";
        if (str!=null) {
            Pattern p = Pattern.compile("\\s*|\t|\r|\n");
            Matcher m = p.matcher(str);
            dest = m.replaceAll("");
        }
        return dest;
    }
}
